Performance optimisations for simulation calculate and parameter lookups#436
Open
nikhilwoodruff wants to merge 6 commits intomasterfrom
Open
Performance optimisations for simulation calculate and parameter lookups#436nikhilwoodruff wants to merge 6 commits intomasterfrom
nikhilwoodruff wants to merge 6 commits intomasterfrom
Conversation
Adds a flat dict[tuple[str,str], array] at the Simulation level, checked at the top of calculate() before tracer, random seed and _calculate() machinery. Only active when map_to=None and decode_enums=False (the inner-loop hot path). Invalidation mirrors the existing holder cache: - purge_cache_of_invalid_values() removes invalidated entries - delete_arrays() removes the relevant key(s) - clone() gets a fresh empty cache to prevent cross-simulation sharing Uses getattr/hasattr guards so StubSimulation and other test subclasses that bypass __init__ work without modification. Co-Authored-By: Claude <noreply@anthropic.com>
Skip the fast path when tracing is enabled, so FullTracer records all calculations correctly. Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
Author
|
Good catch — the fast path was skipping |
Replace O(N×K) numpy.select with O(N) index-based selection in VectorialParameterNodeAtInstant.__getitem__. For enum/EnumArray keys, build a lookup table mapping integer codes directly to child indices, avoiding the intermediate string conversion entirely. For string keys, use numpy.unique to reduce N×K string comparisons to U dict lookups (where U = unique keys, typically ≪ N). Also cache build_from_node results on ParameterNodeAtInstant to avoid rebuilding the recarray on every vectorial access. US household_net_income compute: 12.8s → 9.0s (-30%). Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two sets of performance improvements for the simulation hot path.
1. Fast cache for repeated variable lookups — Flat
dict[(variable_name, str(period)), array]at theSimulationlevel, checked at the very top ofcalculate()before the tracer and full_calculate()machinery. Only active whenmap_to=Noneanddecode_enums=False(the inner-loop hot path used by formulas calling dependencies). Invalidation mirrors the existing holder cache.2. Vectorial parameter lookup optimisation — Replaces O(N×K)
numpy.selectinVectorialParameterNodeAtInstant.__getitem__with O(N) index-based selection. For enum/EnumArray keys, builds a lookup table mapping integer codes directly to child indices, skipping string conversion. For string keys, usesnumpy.uniqueto reduce comparisons. Also cachesbuild_from_noderesults onParameterNodeAtInstantto avoid rebuilding the recarray on every access.US
household_net_incomecompute: 12.8s → 9.0s (-30%). All existing tests pass (tracer test failures are pre-existing).